package crm.controller;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.sql.Date;

import javax.swing.AbstractAction;
import javax.swing.Action;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTable;
import javax.swing.SpringLayout;
import javax.swing.table.DefaultTableModel;

import crm.daoInterfaces.CampaignDao;
import crm.implementor.CampaignImp;
import crm.model.Campaign;
import crm.utility.AppUtility;
import crm.utility.ButtonColumn;
import crm.utility.ViewUtilites;
import crm.view.campaign.CampaignHome;
import crm.view.leeds.FollowupActionsView;
import crm.viewframework.UIFrameWork;
import crm.viewframework.Validater;


@SuppressWarnings("unused")
public class CampaignHomeController implements ActionListener
{
	CampaignHome view;
	Campaign model;
	

	
/******************************************CONSTRUCTOR**************************************************************/
public CampaignHomeController() 
	{
		
	}


	
public CampaignHomeController(CampaignHome view, Campaign model) {
		super();
		this.view = view;
		this.model = model;
		try {
			AppUtility.setComboAtten(view.getComboCreatedBy());
		} catch (Exception e) {
			
			e.printStackTrace();
		}
		view.actionListener(this);
		setGoToColumn();
	}




/******************************************STATIC PART**************************************************************/	
	

	public void reactOnApplyFilter() throws Exception
	{
		String start=view.gettStartDate().getText();
		String created=view.getComboCreatedBy().getSelectedItem().toString();
		CampaignDao imp=new CampaignImp();
		Campaign[]m=imp.retrieveArray("createdBy",created);
		System.out.println("LENGTH IS "+m.length);
		DefaultTableModel dm=(DefaultTableModel) view.getTable().getModel();
		for(int i=0;i<m.length;i++)
		{
			dm.addRow(new Object[]{m[i].getCampaignName(),m[i].getCreatedBy(),m[i].getCampaignStartDate(),m[i].getCampaignEndDate(),m[i].getDescription()});
		}
	}

	
	@SuppressWarnings("serial")
	private void setGoToColumn()
	{
		Action delete = new AbstractAction()
		{
		
			JTable table=view.getTable();
		
			public void actionPerformed(ActionEvent e)
		    {
		       
		    	table = (JTable)e.getSource();
		        int modelRow = Integer.valueOf( e.getActionCommand() );
		       
		        SpringLayout layout=(SpringLayout) view.getParent().getLayout();
		        JPanel panel=(JPanel) view.getParent();
		        FollowupActionsView followup=new FollowupActionsView();
		       // FollowUpActionsController controller=new FollowUpActionsController(followup,lead[modelRow]);
		       // UIFrameWork.setReportComponent(layout,followup,panel,2);
		    }
		};

		ButtonColumn buttonColumn = new ButtonColumn(view.getTable(),delete,5);
		buttonColumn.setMnemonic(KeyEvent.VK_D);
	}
	
/******************************************DYNAMIC PART**************************************************************/	
	
	@Override
	public void actionPerformed(ActionEvent e) 
	{
		String k=e.getActionCommand();
		if(k.contains("Apply Filter"))
		{
			if(validates())
			{
				try 
				{
					reactOnApplyFilter();
				} 
				catch (Exception e1) 
				{
				
					e1.printStackTrace();
				}
			}
		}
		
	}
	
	private boolean validates()
	{
		Validater validates = new Validater(view);
		 String message = validates.validateForEmpty();
		 if(message.equals("")==false)
		 {
			 JOptionPane.showMessageDialog(null, message);
			 return false;
			 
		 }
		 return true;
	}

}
/*******************************************************************************************************************/